
import debug from "@/_js/debug"; 
export default {

  /** 冒泡排序
   * 
   * @param {Array} datas 排序数组
   * @param {*} key 排序关键字, undefined时，直接排序内容
   * @param {*} dire 1上大下小  2上小下大
   * @returns 
   */
  maoPao(datas, key, dire){
    datas = JSON.parse(JSON.stringify(datas));
    
    for (let i = 0; i < datas.length; i++) {
      var length = datas.length - i - 1;
      for (let j = 0; j < length; j++) {
        var pair1 = datas[j];
        var pair2 = datas[j+1];
        var value1 = key ? parseFloat(pair1[key]) : parseFloat(pair1);
        var value2 = key ? parseFloat(pair2[key]) : parseFloat(pair2);
        if (value1 > value2) {
          datas[j] = (dire == 1) ? pair1 : pair2;
          datas[j + 1] = (dire == 1) ? pair2 : pair1;
        } else {
          datas[j] = (dire == 1) ? pair2 : pair1;
          datas[j + 1] = (dire == 1) ? pair1 : pair2;
        }
      }
    }
    return datas;
  },
}